<?php
/**
 * @version SVN: $Id$
 * @package    fmcSeminaire
 * @subpackage Base
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Ludovic Launer {@link }
 * @author     Created on 09-Nov-2010
 * @license    GNU/GPL
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport('joomla.application.component.controller');

/**
 * fmcSeminaire default Controller
 *
 * @package    fmcSeminaire
 * @subpackage Controllers
 */
class fmcSeminaireController extends JController
{
    /**
     * Method to display the view
     *
     * @access	public
     */
    function display()
    {
        parent::display();
    }// function
    
static function getCampagneId()
{
	// --- Get Campagne Id ---
	// --- Try menu parameter
    $params = &JComponentHelper::getParams( 'com_fmcseminaire' );
    $campagneId = $params->get( 'campagne' );
    
	return $campagneId;
}
    
function sbmitWishes()
    {
    global $mainframe;
    $model =& $this->getModel();
    
    $result = array('success'=>false,'message'=>'??');
    $submitedSeminaires = null;
    $submitedDates		= null;
    $seminaires 		= array();	
    $dates				= array();
    
    // ---------- Retrieve submited data ----------
    $post = JRequest::get( 'POST' );
    $submitedSeminaires = $post['submitedSeminaires'];
    $submitedDates		= $post['submitedDates'];
    
    // --- Vote
    $vote = new stdClass;
  	$vote->voteId 		= NULL;
  	$vote->nom 			= $post['name'];
  	$vote->email 		= $post['email'];
  	$vote->codeAdeli 	= $post['codeadeli'];
  	$vote->campagneId	= $post['campagneId'];
  	
  	// --- Seminaires
  	if ($submitedSeminaires != null) {
  		foreach($submitedSeminaires as $id) :
  			$vote_has_seminaire = new stdClass;
  			$vote_has_seminaire->vhsId			= NULL;
  			$vote_has_seminaire->voteId			= NULL;
  			$vote_has_seminaire->seminaireId	= $id;
  			$seminaires[] = $vote_has_seminaire;
  		endforeach;
  	}
    // --- Dates
    
    if ($submitedDates != null) {
  		foreach($submitedDates as $ids) :
  			$vote_has_date = new stdClass;
  			$vote_has_date->vhdId			= NULL;
  			$vote_has_date->voteId			= NULL;
  			$vote_has_date->dateId			= $ids;
  			$dates[] = $vote_has_date;
  		endforeach;
  	}
  	
   
   	//---------- Data Validation -----------------------------------------------
   	$data_validated = true;
   	// --- Check name and email not null (should not occur because of client side validation)
   	
   	// --- Check at least 1 seminaire + 1 date
   	if ($submitedDates==null || $submitedSeminaires == null) {
   		$result = array('success'=>false,'message'=>'Vous devez selectionner au moins 1 Seminaire et 1 Date');
   		$data_validated = false;
   		}
    // --- Check email has not previously voted
    if ($data_validated) {
    	$campagneName = null;
	    $voteAlreadySubmited = $model->isVoteAlreadySubmited($vote->email, $vote->campagneId, &$campagneName);
	    if ($voteAlreadySubmited) {
	    	$result = array('success'=>false,'message'=>$vote->email . ' : Vous avez deja vote precedemment pour ' . $campagneName, 'stop'=>true);
	    	$data_validated = false;
	    	}
    }
   	// ---------- Store into Database -----------------------------------------------
    if ($data_validated) {	
    try {
    	$model->submitWishes($vote, $seminaires, $dates );					// Store into DB
    	$result = array('success'=>true,'message'=>"OK");
    	}
   	catch (Exception $e) {
   		$result = array('success'=>false,'message'=>$e->getMessage());
		}
    }
   
 	echo json_encode($result);
   	$mainframe->close();
    }    
    
}// class
